package com.sixiaolong.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sixiaolong.common.BaseContext;
import com.sixiaolong.common.R;
import com.sixiaolong.domain.ShoppingCart;
import com.sixiaolong.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

//    获取购物车内商品的集合
    @GetMapping("/list")
    public R<List<ShoppingCart>> getAllCart(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);
        return R.success(shoppingCarts);
    }

//    添加购物车
    @PostMapping("/add")
    @Transactional
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据",shoppingCart);
//        1:先把用户id放进去   好知道是那个用户在操作
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
//       2：根据情况  如果菜品为空 则查套餐    如果套餐为空则查 菜品    如果都为空则直接添加

        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(lqw);
//       3： 如果查询到的不为null的话  那么就说明有套餐或者菜品  则对应数量加起来
        if (shoppingCart1!=null){
//            两个数量相加
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartService.updateById(shoppingCart1);
            return R.success(shoppingCart1);
        }
//        3:如果是菜品和套餐都是空的话  直接添加
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }

//    清空购物车
    @DeleteMapping("/clean")
    public R deleteCart(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清除成功");
    }

//    勾选减少商品
    @PostMapping("/sub")
    public R subCar(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
//        2:根据菜品为空 则查套餐   套餐为空查菜品
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
//        如果能查出来  则表示还有数量
        if (cartServiceOne!=null){
            cartServiceOne.setNumber(cartServiceOne.getNumber()-1);
            shoppingCartService.updateById(cartServiceOne);
            return R.success(cartServiceOne);
        }
        return R.error("数量不能小于0");
    }
}
